<?php

class OwnAction extends fvAction {

	function __construct () {
		parent::__construct(fvSite::$Layoult);
	}

	function executeIndex() {
		if (fvRequest::getInstance()->isXmlHttpRequest())
		return self::$FV_NO_LAYOULT;
		else return self::$FV_OK;
	}

	function executeSave() {
		$LoggedUser = fvSite::$fvSession->get ( "login/loggedUser" );
		$User = UserManager::getInstance()->getByPk($LoggedUser->getPk());
		$request = fvRequest::getInstance ();
		$m = $request->getRequestParameter("m");
		$birth = $request->getRequestParameter("birth");
		$m["birth_date"] = $birth['Date_Year'].'-'.$birth['Date_Month'].'-'.$birth['Date_Day'];
		if (!$User->isNew() && (strlen($m['password']) == 0)) {
            unset($m['password']);
            unset($m['password1']);
        }
        if (empty($m['gender'])){
        	$User->setValidationResult("gender", false, "Необходимо указать пол");
        }
		if (empty($m['city'])){
        	$User->setValidationResult("city", false, "Необходимо указать город");
        }
		if (empty($m['visited_countries'])){
        	$User->setValidationResult("visited_countries", false, "Необходимо указать список посещённых стран");
        }
		$User->updateFromRequest ( $m , array('active', 'is_root', 'inherit', 'permitions'));
		if ($User->save()) {
			$mail = new html_mime_mail();
			$letter = "<html><body>Ваши персональные данные на сайте <a href=\"http://turtess.tv\">www.turtess.tv</a> были изменены.</body></html>";
			$mail->add_html($letter);
			$mail->build_message("w");
			$mail->SendMail('noreply@turtess.tv','Администрация',$m[mail],$m[surname]." ".$m[name],"Регистрация на сайте" );
				
		} else {
			fvSite::$fvSession->set ( "ERROR", $User->getValidationResult () );
		}
		$this->redirect ( "@own/edit/" );
	}	
	
}	
?>